
#include "stdafx.h"
#include "WicBitmapSource.h"

//==============================================================================
// CWicBitmapSource
//==============================================================================
CWicBitmapSource::CWicBitmapSource()
{
    ClearPtr();
}

CWicBitmapSource::~CWicBitmapSource()
{
    if(m_pIWICBitmapSource)
    {
        m_pIWICBitmapSource->Release();
    }
    m_pIWICBitmapSource = NULL;
}

CWicBitmapSource::CWicBitmapSource(CWicBitmapSource const & objBitmapSource)
{
    ClearPtr();
    m_pIWICBitmapSource = objBitmapSource.m_pIWICBitmapSource;
    m_pIWICBitmapSource->AddRef();
}

CWicBitmapSource& CWicBitmapSource::operator=(CWicBitmapSource const & objBitmapSource)
{
    if(m_pIWICBitmapSource)
    {
        m_pIWICBitmapSource->Release();
    }
    ClearPtr();
    m_pIWICBitmapSource = objBitmapSource.m_pIWICBitmapSource;
    m_pIWICBitmapSource->AddRef();
    return *this;
}

//==============================================================================
// IWICBitmapSource
//==============================================================================
HRESULT CWicBitmapSource::GetSize(
                        /* [out] */ __RPC__out UINT *puiWidth,
                        /* [out] */ __RPC__out UINT *puiHeight)
{
    g_hrLast = GetPtr()->GetSize(puiWidth, puiHeight);
    return g_hrLast;
}

SIZE CWicBitmapSource::GetSize()
{
    UINT uWidth = 0;
    UINT uHeight = 0;
    g_hrLast = GetPtr()->GetSize(&uWidth, &uHeight);
    SIZE size = {0, 0};
    if(g_hrLast == S_OK)
    {
        size.cx = uWidth;
        size.cy = uHeight;
    }
    return size;
}

HRESULT CWicBitmapSource::GetPixelFormat(
                               /* [out] */ __RPC__out WICPixelFormatGUID *pPixelFormat)
{
    g_hrLast = GetPtr()->GetPixelFormat(pPixelFormat);
    return g_hrLast;
}

HRESULT CWicBitmapSource::GetResolution(
                              /* [out] */ __RPC__out double *pDpiX,
                              /* [out] */ __RPC__out double *pDpiY)
{
    g_hrLast = GetPtr()->GetResolution(pDpiX, pDpiY);
    return g_hrLast;
}

HRESULT CWicBitmapSource::CopyPalette(
                            /* [in] */ IWICPalette *pIPalette)
{
    g_hrLast = GetPtr()->CopyPalette(pIPalette);
    return g_hrLast;
}

HRESULT CWicBitmapSource::CopyPixels(
                           /* [unique][in] */ const WICRect *prc,
                           /* [in] */ UINT cbStride,
                           /* [in] */ UINT cbBufferSize,
                           /* [size_is][out] */ __RPC__out_ecount_full(cbBufferSize) BYTE *pbBuffer)
{
    g_hrLast = GetPtr()->CopyPixels(prc, cbStride, cbBufferSize, pbBuffer);
    return g_hrLast;
}

IWICBitmapSource* CWicBitmapSource::GetPtr()
{
    if(m_pIWICBitmap)
    {
        return m_pIWICBitmap;
    }
    else if(m_pIWICBitmapFlipRotator)
    {
        return m_pIWICBitmapFlipRotator;
    }
    else if(m_pIWICFrameDecode)
    {
        return m_pIWICFrameDecode;
    }
    else if(m_pIWICFormatConverter)
    {
        return m_pIWICFormatConverter;
    }
    return m_pIWICBitmapSource;
}

void CWicBitmapSource::ClearPtr()
{
    m_pIWICBitmap = NULL;
    m_pIWICBitmapSource = NULL;
    m_pIWICBitmapFlipRotator = NULL;
    m_pIWICFrameDecode = NULL;
    m_pIWICFormatConverter = NULL;
}


